package com.ljkj.blockpay.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.IdRes;

/**
 * Created by yp on 2022/3/1
 */
public class AlertController {
    private CommonDialog dialog;
    private Window window;

    private DialogViewHelper mViewHelper;

    public AlertController(CommonDialog dialog, Window window) {
        this.dialog = dialog;
        this.window = window;
    }


    public void setViewHelper(DialogViewHelper viewHelper){

        this.mViewHelper = viewHelper;

    }

    /**
     * 设置文本
     * @param viewId
     * @param text
     */
    public void setText(@IdRes int viewId, CharSequence text){
        mViewHelper.setText(viewId,text);
    }

    /**
     * 获取view
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(int viewId) {
        return mViewHelper.getView(viewId);
    }

    /**
     * 设置点击事件
     *
     * @param viewId
     * @param listener
     */
    public void setOnclickListener(int viewId, View.OnClickListener listener) {
        mViewHelper.setOnclickListener(viewId, listener);
    }

    public void setGone(int viewId, int visibility) {
        mViewHelper.setGone(viewId, visibility);
    }

    /**
     * 获取Dialog
     *
     * @return
     */
    public CommonDialog getDialog() {
        return dialog;
    }

    /**
     * 获取Dialog的Window
     *
     * @return
     */
    public Window getWindow() {
        return window;
    }


    public static class AlertParams{

        public Context mContext;
        public int mThemeResId;
        // 点击空白是否能够取消  默认点击阴影不可以取消
        public boolean mCancelable = false;
        // dialog Cancel监听
        public DialogInterface.OnCancelListener mOnCancelListener;
        // dialog Dismiss监听
        public DialogInterface.OnDismissListener mOnDismissListener;
        // dialog Key监听
        public DialogInterface.OnKeyListener mOnKeyListener;
        // 布局View
        public View mView;
        // 布局layout id
        public int mViewLayoutResId;
        // 存放字体的修改
        public SparseArray<CharSequence> mTextArray = new SparseArray<>();
        // 存放点击事件
        public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
        //存放view的显示与隐藏
        public SparseArray<Integer> mVisibility = new SparseArray<>();
        // 宽度
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 动画
        public int mAnimations = 0;
        // 位置
        public int mGravity = Gravity.CENTER;
        // 高度
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;

        public AlertParams(Context context, int themeResId) {
            this.mContext = context;
            this.mThemeResId = themeResId;
        }


        /**
         * 绑定和设置参数
         *
         * @param mAlert
         */
        public void apply(AlertController mAlert) {
            DialogViewHelper viewHelper = null;
            if (mViewLayoutResId != 0) {
                viewHelper = new DialogViewHelper(mContext, mViewLayoutResId);
            }

            if (mView != null) {
                viewHelper = new DialogViewHelper();
                viewHelper.setContentView(mView);
            }
            if (viewHelper == null) {
                throw new IllegalArgumentException("请设置布局setContentView()");
            }
            mAlert.getDialog().setContentView(viewHelper.getContentView());

            mAlert.setViewHelper(viewHelper);

            int textArraySize = mTextArray.size();
            for (int i = 0; i < textArraySize; i++) {
                mAlert.setText(mTextArray.keyAt(i), mTextArray.valueAt(i));
            }

            int clickArraySize = mClickArray.size();
            for (int i = 0; i < clickArraySize; i++) {
                mAlert.setOnclickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
            }

            int visibility = mVisibility.size();
            for (int i = 0; i < visibility; i++) {
                mAlert.setGone(mVisibility.keyAt(i), mVisibility.valueAt(i));
            }

            Window window = mAlert.getWindow();
            window.setGravity(mGravity);


            if (mAnimations != 0) {
                window.setWindowAnimations(mAnimations);
            }

            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);
        }
    }
}
